<?

    class Lib {
        
        var $partialcache   = array();
        var $language       = 'de';
        var $translation    = false;
        var $page_base_url  = false;
        var $languages      = array('de','en');
        var $learn_language = false;
        
        //
        public function __construct(){
            // get the url base host    
            $this->page_base_url    = 'http://'.$_SERVER['HTTP_HOST'].''.str_replace('index.php','',$_SERVER['PHP_SELF']);
            
            // get url slug
            $slug                   = $this->getUriPages();
            
            // set language
            if($slug[0]){
                if(in_array($slug[0],$this->languages)){
                    $this->language = $slug[0];
                }
            }
            
            // first try
            $this->translation = $this->getTranslation();
            
            // set to default, if no translation was found
            if(!$this->translation){
                $this->language = 'de';
                $this->translation = $this->getTranslation();    
            }

        }
        
        
        //
        public function partial($page,$action=array(),$path=false){
            global $_p; 
            
            if(!$page)
                return;
                
            if($path==false){
                $file = './assets/docu/'.$page.'.phtml';

                if(!file_exists($file))
                    $file = './assets/docu/'.$page.'.phtml';
                
            } else {
                $file = $path.$page.'.phtml';
            }
            
            $key = md5($file);
            
            if(file_exists($file)){
                if(!$this->partialcache[$key]){
                    $source = implode(file($file));
                    $this->partialcache[$key] = $source;
                }
                
                ob_start();
                eval("?>" . $this->partialcache[$key] . "<?");
                $c = ob_get_contents();
                ob_end_clean();
                
                if($action===true){
                    return htmlentities($c);
                } else {
                    return $c; 
                }
            } 
        }

        
        public function t($s){
            
            if(!$this->translation)
                $this->translation = array();
                        
            if(!in_array($s , array_keys($this->translation) )){
                $this->translation[$s] = $s;
                return $s;
            } else {
                return $this->translation[$s];
            }

            
        }
        
        public function getUriPages($filename=false,$as_sring=false){
            
            if($filename==false)
                $filename = 'index.php';
            
            $filter = 'abcdefghijklmnopqrstuvwxyz0123456789@./+-_ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            
            $base   = str_replace($filename,'', urldecode($_SERVER['PHP_SELF']));
            
            if($base!='/') { 
                $uri    = str_replace($base, '', urldecode($_SERVER['REQUEST_URI']));
            } else {
                $uri    = substr(urldecode($_SERVER['REQUEST_URI']),1,strlen(urldecode($_SERVER['REQUEST_URI'])));
            }

            $ux = explode('?',$uri);
            if(count($ux>1))
                $uri=$ux[0];
            
            for($u=0; $u<strlen($uri); $u++){
                for($f=0; $f<strlen($filter); $f++){
                    if($uri{$u}==$filter{$f}){
                        $ub.=$uri{$u};
                    }
                }
            }
            
            if($ub{strlen($ub)-1}=='/') $ub = substr($ub, 0, -1);
            return explode('/', $ub);
        }

        public function generateHashKey($array, $length=4, $key=false){
            if($key==false){
                $s= 'BCDFGHJKLMNPRSTVXYZ';
                $r= '';
                for($i=0; $i < $length; $i++){
                    $r.=$s{rand(0,(strlen($s)-1))};
                }
                $key = $r;
            }
            
            if( in_array($key,array_keys($array))){
                return $this->generateIdent($array, $length, $r);
            } else {
                return $key;  
            }
        }
        
        public function setTranslation(){
            
            if(is_array($this->translation)){
                $translation_data = (json_encode($this->translation));
                
                $fh = fopen('assets/docu/lang/'.$this->language.'.json',"w");
                
                fwrite($fh,$translation_data);
                fclose($fh);
                
                return true;  
            }
            
        }
        
        public function getTranslation(){
            $json_filename = 'assets/docu/lang/'.$this->language.'.json';
            
            if(file_exists($json_filename))
               $translation_data = file($json_filename);
            
            if($translation_data) {
                return (array)json_decode( implode($translation_data));
            } else {
                return false;
            }
        }
        
        
    }
    
?>